<template>
  <div class="app-container">
    <div class="filter-container">
      <el-input
        v-model="listQuery.name"
        placeholder="请输入搜索内容"
        style="width: 200px;"
        class="filter-item"
        @keyup.enter.native="handleFilter"
      />
      <el-select
        v-model="listQuery.type"
        placeholder="搜索类型"
        clearable
        class="filter-item"
        style="width: 130px"
      >
        <el-option
          v-for="item in searchTypeOptions"
          :key="item.key"
          :label="item.display_name+'('+item.key+')'"
          :value="item.key"
        />
      </el-select>
      <el-select
        v-model="listQuery.sort"
        style="width: 140px"
        class="filter-item"
        @change="handleFilter"
      >
        <el-option
          v-for="item in sortOptions"
          :key="item.key"
          :label="item.label"
          :value="item.key"
        />
      </el-select>
      <el-button
        v-waves
        class="filter-item"
        type="primary"
        icon="el-icon-search"
        @click="handleFilter"
      >搜索</el-button>
    </div>

    <el-table
      :key="tableKey"
      v-loading="listLoading"
      :data="list"
      border
      fit
      highlight-current-row
      style="width: 100%;"
      @sort-change="sortChange"
    >
      <el-table-column
        label="ID"
        prop="_id"
        sortable="custom"
        align="center"
        width="200"
        :class-name="getSortClass('_id')"
      >
        <template slot-scope="{row}">
          <span>{{ row._id }}</span>
        </template>
      </el-table-column>
      <el-table-column label="生产日期" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.date | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="保质期" width="150px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.warranty | parseTime('{y}-{m}-{d} {h}:{i}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="商品名" min-width="150px" align="center">
        <template slot-scope="{row}">
          <span class="link-type">{{ row.name }}</span>
          <el-tag>{{ row.type | typeFilter }}</el-tag>
        </template>
      </el-table-column>
      <el-table-column label="商品图片" min-width="150px" align="center">
        <template slot-scope="{row}">
          <el-image 
          style="height:100px" 
          :src="imgsUrl+row.imgs[0]"
          
          ></el-image>
        </template>
      </el-table-column>
      <el-table-column label="商品分类" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.clazz }}</span>
        </template>
      </el-table-column>
      <el-table-column label="材质" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.material }}</span>
        </template>
      </el-table-column>
      <el-table-column label="制作方法" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.method }}</span>
        </template>
      </el-table-column>
      <el-table-column label="适用规格" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.standard }}</span>
        </template>
      </el-table-column>
      <el-table-column label="独家" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.exclusive }}</span>
        </template>
      </el-table-column>
      <el-table-column label="口味" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.flavor }}</span>
        </template>
      </el-table-column>
      <el-table-column label="特殊功用" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.special }}</span>
        </template>
      </el-table-column>
      <el-table-column label="产地" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.origin }}</span>
        </template>
      </el-table-column>
      <el-table-column label="特色" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.feature }}</span>
        </template>
      </el-table-column>
      <el-table-column label="包装" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.packing }}</span>
        </template>
      </el-table-column>
      <el-table-column label="价格" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.price }}</span>
        </template>
      </el-table-column>
      <el-table-column label="供应商" width="110px" align="center">
        <template slot-scope="{row}">
          <span>{{ row.author }}</span>
        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :curPage.sync="listQuery.curPage"
      :eachPage.sync="listQuery.eachPage"
      @pagination="getList"
    />
  </div>
</template>

<script>
import {
  getAllFoods
} from "@/api/zjm_Foods";
import waves from "@/directive/waves"; // waves directive
import { parseTime } from "@/utils";
import Pagination from "@/components/Pagination"; // secondary package based on el-pagination

const searchTypeOptions = [
  { key: "name", display_name: "商品名" },
  { key: "type", display_name: "商品类型" },
  { key: "status", display_name: "商品状态" },
  { key: "clazz", display_name: "商品分类" },
  { key: "material", display_name: "材质" },
  { key: "method", display_name: "制作方法" },
  { key: "standard", display_name: "适用规格" },
  { key: "exclusive", display_name: "独家" },
  { key: "flavor", display_name: "口味" },
  { key: "special", display_name: "特殊功用" },
  { key: "origin", display_name: "产地" },
  { key: "feature", display_name: "特色" },
  { key: "packing", display_name: "包装" },
  { key: "price", display_name: "价格" }
];
const calendarTypeOptions = [
  { key: "主粮", display_name: "主粮" },
  { key: "洗浴", display_name: "洗浴" },
  { key: "日用品", display_name: "日用品" },
  { key: "零食", display_name: "零食" },
  { key: "保健品", display_name: "保健品" },
  { key: "医疗", display_name: "医疗" },
  { key: "清洁洗护", display_name: "清洁洗护" },
  { key: "玩具", display_name: "玩具" }
];

// arr to obj, such as { CN : "China", US : "USA" }
const calendarTypeKeyValue = calendarTypeOptions.reduce((acc, cur) => {
  acc[cur.key] = cur.display_name;
  return acc;
}, {});

export default {
  name: "ComplexTable",
  components: { Pagination },
  directives: { waves },
  filters: {
    statusFilter(status) {
      const statusMap = {
        published: "success",
        draft: "info",
        deleted: "danger"
      };
      return statusMap[status];
    },
    typeFilter(type) {
      return calendarTypeKeyValue[type];
    }
  },
  data() {
    return {
      fileList: [],
      imgsUrl: "http://localhost:3001",
      tableKey: 0,
      list: null,
      total: 0,
      listLoading: true,
      listQuery: {
        curPage: 1,
        eachPage: 20,
        name: undefined,
        type: undefined,
        sort: +1
      },
      calendarTypeOptions,
      searchTypeOptions,
      sortOptions: [
        { label: "ID 升序", key: +1 },
        { label: "ID 降序", key: -1 }
      ],
      textMap: {
        update: "修改",
        create: "新增"
      },
      dialogPvVisible: false,
    };
  },
  created() {
    this.getList();
  },
  methods: {
    async getList() {
      //获取货品列表
      this.listLoading = true;
      let data = await getAllFoods(this.listQuery);
      data.rows.map(v => {
        v.standard = v.standard.join(",");
        v.packing = v.packing.join(",");
        v.flavor = v.flavor.join(",");
        v.price = v.price.join(",");
      });
      this.list = data.rows;
      this.total = data.total;
      setTimeout(() => {
        this.listLoading = false;
      }, 1000);
    },
    handleFilter() {
      this.listQuery.page = 1;
      this.getList();
    },
    sortChange(data) {
      const { prop, order } = data;
      if (prop === "_id") {
        this.sortByID(order);
      }
    },
    sortByID(order) {
      if (order === "descending") {
        this.listQuery.sort = -1;
      } else {
        this.listQuery.sort = +1;
      }
      this.handleFilter();
    },
    getSortClass: function(key) {
      const sort = this.listQuery.sort;
      return sort === `+${key}` ? "+1" : "-1";
    }
  }
};
</script>
